I think the best way to explain this is by walking through my thought process as I was writing my last novel, Someone Will Save You, where my protagonist was asexual.
When I was writing this book, the vast majority of the scenes have nothing to do with Luna being asexual. For example, there’s a scene where her mother tries to talk her into being exorcised (long story). This is completely unrelated to Luna’s sexuality, gender, really anything. Luna could be straight, gay, bi, ace, trans, non-binary, any race, etc. You catch my drift. The scene would be the same. As I was writing this scene I was not thinking about the fact that Luna was asexual, I was thinking about who Luna is as a person and how she would act and react based on her personality.
I was only thinking about Luna’s asexuality in scenes where it was actually relevant to the scene. For example, a scene where she comes out to a new friend. However, the most important thing is that I was not thinking “what would an asexual person do here?” but rather “what would Luna, who is an asexual person, do here?” All asexual people are different, so would react differently here.
You can never sum up an entire group with a single character. You can never represent an entire group with a single character. To think you can is to assume that people of this group are not complex and all the same. However, that doesn’t mean that most people of this group can’t relate to that character. Luna is (I hope at least cause I tried) a complex character, and she would be a complex character no matter her sexuality.
Yes, you need to use empathy and research to write a character who is marginalized in a way you are not, but the most important part is that you always view them as a person and not simply the label that describes how they are different from you.
The takeaway here is that if your characterization is flat you might end up relying on stereotypes. To represent a group you’re not in, you need a well developed character.
You do need to do your research in order to avoid stereotypes, but I feel that the reason characters most often become offensive is because the writer doesn’t develop them enough beyond their marginalization. You’ll need to pass the book by people of that group, whether someone you know or sensitivity readers. Be open to that feedback and adjust the book accordingly if you do write the character offensively. However, I think that a crippling fear of ‘getting the character wrong’ can actually lead to more stereotyped character development because the writer might rely solely on their understanding of the group as a whole and not the individual character.