It's definitely doable. But you have to thread carefully. There's plenty of anecdotes of DMs doing this and the results being not great.
I've done it a couple of times.
I also know, without having listened to it, that one of Critical Role's short campaign with Brendan had a similar dynamic going on. They were roleplaying through some world-changing events that they couldn't stop. It created lots of discussions online.
My opinion is that having a certain outcome be fixed (in this way) is not at odds with player agency. Player agency is about being able to make choices, and these choices being real and having an effect. So maybe they can't answer the question "Can we win this fight?" but there are other questions that can be answered. "How little do we lose?" could be an example. There's plenty of agency to be had.
If it's a fight that's just impossible. Either because it's conceptually impossible, or mathematically impossible. Let's say they're fighting God. There's plenty of ways to be obvious and let them know ahead. The presence of that being shakes you inside, you feel disarm and hopeless and you know in your heart that you can't do anything to it. It's fitting.
It's there's a little bit more interpretation to be had... like a foe that's just too strong at this point in the adventure, but will be defeatable later, that's where the water's muddy. The foe might be defeatable mathematically. It has hit points, the players can land hits, it's just very unlikely. I definitely lean on the side of being clear to players: it's hard to tell, but this foe is too big a threat for your group.
I think it can be done gracefully. But I'll be honest, the handful of times that I tried it, it never backfired, but I don't think it really added much. It's much cooler on paper than in play.
I tend to allow the dice to decide of what happens, and just telegraph clearly to the players when it's an abnormally difficult fight. It might happen because I really let them do whatever they want. If they decide to chase a mighty dragon at level 1, I'm not gonna move things in my notes to make sure they don't find it. I'll be clear that mathematically it's very unlikely that they don't all die, but if they want to proceed, they can.