D&D is an RPG, a role playing game. Characters play a role in a story.
When we experience stories (whether a movie, a TV series, a book or a play) and the characters do not act 'in character', it diminishes the quality of the story. It takes us out of the story. When it happens in an RPG with a good story, it takes us out of the story and makes immersion harder - and nearly universally, when players are actually immersed in a story, it is a better experience.
Yes, even the people that just want to play hack and slash with no story ... if you accidentally get caught up in a good story while you are playing, it will be a better experience for you. I base this on decades and decades of experience with perhaps a thousand different players. And, even if there is one player at the table that has no appreciation for the story while the rest does - that player playing into the story and playing in character will make the experience better for the rest of the team, so it will be better overall for everyone overall if they do play in character rather than just ignoring character.
HOWEVER: This is a game. The real world sometimes trumps the game. These are a few places where I 100% support a player putting out of game concerns before in game character decisions:
1.) Uncomfortable: If a player is uncomfortable with something going on in the game (in character), the situation needs to be addressed. As a DM, if a player indicates to me that they're uncomfortable with a storyline, I talk with them about what we can do about it. Options include, "Fade to black" in which we stop the action and then pick up the story down the road after the situation has resolved; "Savior" where a third party will come in and put an end to the situation then and there so that we can just end the uncomfortable thing right away, "Sit out" where the player steps away from the game until the uncomfortable element is resolved, "Diversion" to have a sudden change in the situation that pulls the group away from the uncomfortable situation; and "Undo" where we rewind the game and avoid the situation by making different decisions.
If I have a player that has provided guidance that there are elements of the campaign that edge on discomfort for the player, I'll sometimes build in a ripcord for them. For example, I put an ornate wooden box into a treasure pile that a PC found first and - out of character - let the player know that it was there as a rip cord for them if things were making them uncomfortable. When opened, the box took the entire group to the Feywild. There, the passage of time would be different, allowing the situation that was uncomfortable to resolve without the PCs being present for it in the Prime.
2.) Design: Some of the fun things about D&D are the combinations of abilities. Sometimes you just want to put together a fun combination of Artificer, Sorcerer and Druid powers. When a player has such an idea, I ask them to figure out the story as to why that combination would come into place, and we weave the outline of a story that gets them where they want to go. Then, as a DM, I'll build story elements to give them the option to proceed along that path ... but in my experience, a lot of players will drop these plans as they advance their PCs in favor of new goals or new ideas. Here, we're putting the out of game design literally BEFORE the in game character development - but both happen.
3.) Boredom: Some storylines just don't work. Occasionally, as a DM, you'll look around the table and realize that - like any comedian - your current build is just not working for the players. Essentially, there is no quality to the story for the group. One trick I have, as a DM, is that I always have lurking background storylines that could manifest as an ambush at any moment, and I always have a deadly ambush encounter ready to go. One feature of these ambushes is usually an environment changing mechanic - like reverse gravity, lava springing up from the floor, pits of unknown darkness, etc... If I'm losing the players and the current storyline is a dud, I'll pull the rip cord on a solution like the unexpected ambush. It is usually quite obvious what I'm doing - which is realizing out of game that the current situation is not working and then doing something without specific storyline / in character(s) motivation to shake things up as a DM, but if you make the encounter interesting enough, it can pull people back in for the rest of the session while you adjust as a DM to resolve it.
4.) Player to Player Issues: People date, break up, get in fights, etc... when the people have issues at my table, I speak with them individually and then decide what to do (usually with the input of the rest of the group). This can have major story impacts as groups may split in two, or we may see PCs drop in and out to avoid the players being there at the same time. I adjust and figure something out.
In one situation, I had a curse laid on the PCs so that they could never see each other again and one was shunted to a pocket dimension while the other was outside of it, and then they 'randomly' switched back and forth. Then, I gave them each 100 plastic chips and told them every session I'd add 10 more to each of their stashes. Between sessions, they could silently bid on who would be there for the next session - winner paid, loser kept their stash, and could use it the next time. It started out rough, and there were some definite 'screw you' spends (such as one of them spending their entire stash to be present when we played on the other's birthday), but it worked for those people overall.