Inappropriate Humor Term
There's a term I've been trying to remember that defines a moment where a serious story or scene ends with a joke or light ending. We see it in TV shows like Star Trek (TOS) where an otherwise grim story has been ended with a joke.
The term could be psycho-analytical. It's possibly German in origin, but that may not be accurate.
It isn't schadenfreude, macabre, dark humor, black humor, black comedy. It's a term meant to signify the writer's apparent idiosyncratic need to end a dark/grim story with a joke.
Writers of Star Trek were known for this. So is Joss Whedon.
This is not necessarily a form "bad" writing. But it Is considered inappropriate.
Solution 1:
It can be described as comic relief:
comic episodes in a dramatic or literary work that offset more serious sections.
Oxford Dictionaries/Lexico
Note that comic relief is considered a legitimate literary technique, although (like anything) it can be misused.
See also Wikipedia.