**The DM and the players should communicate**

First of all, not all groups have similar opinions on encounters. Some players like hack and slash and just killing enemies to feel cool and powerful. Some groups like gritty meat grinder encounters and find TPKs normal. While other groups dislike encounters for the sake of encounters and prefer every encounter to have narrative importance.

If the players want to feel cool, the DM should "shoot the monk" so design the encounter in a way that lets the players use their abilities. Enemy tactics that rely on disengaging for the Sentinel feat player, hordes of small enemies for the AoE damage dealer, etc

If the players want a meat grinder, there is no need to be afraid of TPKs. If the players want encounters with narrative importance, then the DM should think of reasons why the NPCs would attack the players and roleplay the NPCs.

The players should tell the DM what they want if the DM doesn't communicate. If you think encounters are too hard and ruining the fun, you should tell the DM that. By the way, XP/CR is a poor way to balance encounters.

**Action economy, environment and enemy tactics**

CR/XP does not account for enemies using tactics, enemies having tools, particular terrain, etc. Very difficult encounters can be designed using only low-CR enemies by making proper use of terrain, hiding places for the enemies and giving the enemies a strategy. Speaking of enemy strategy, enemies can have a goal that is not necessarily "kill all players" (it can go from defending their territory, obtaining whatever item the players have, stalling/distracting the players, etc)

If the enemies are in their territory and are prepared, they can have strategic positioning, trick the players into triggering traps during the encounter, have advantage on their attack rolls by using surprise/hiding/magical darkness (if the enemies have blindsight or devil's sight or something). To further give the enemies an edge, they could have collected intel on the players and their abilities/strategies.

All this is to say that encounters can be more about how the monsters are used and less about the monster's raw stats. A high CR monster that is a dumb brute will most of the time be trounced by the players.

**Setting goals other than "kill all enemies"**

The players and the enemies probably have goals other than wiping the other side out. The players might have a time sensitive mission and the enemies are here to stall/distract the players. Maybe the players have a target to protect (an object or NPC with a certain amount of health) and the enemies have to damage that target. Maybe the players have to reach a location (a point on the map) and the enemies have to prevent the players from doing so. Or swap the enemies and the players, now the enemies are defending a target and the players are trying to destroying it.

Creating such goals will vary both player and enemy strategies and add a different type of difficulty to the encounter. Strategies might focus on tricking or taunting the others side instead of just dealing damage.

**The DM should tell players that they can always run away, and the players should remember that they can run away if encounters are too tough**

Running away from monsters is a perfectly valid choice, it's logical (the characters would have self preservation instincts). You might feel better if you made the DM aware that the group will run away from encounters that look too difficult and the DM lets the group do that.