I think when it's fanfiction about boybanders, it arises out of a young girl's crush on an unattainable idol. She can't meet the One Direction boys or the K-pop band in real life, but she's still experiencing an emotional journey, in her crush on them. That needs to play itself out in some kind of trajectory. For some girls, that's hating their real life girlfriends or going into meltdown when the band breaks up. For others it might be finding a boy who looks a bit like their crush and playing out a real relationship with him. For others, it seems to involve constructing a personality for the love interest inside their head, and reenacting the beats of a relationship through fiction.
With fiction based on an actual fictional property, I think it's coming from a slightly different place, and is more strongly tied to escapism. Sometimes it comes from over-identifying with a character. It's not necessarily even that they have a crush on this character. The character could be female, and they're straight, or male, and the girl is gay. But there is some element of this character the girl finds so relatable she sort of 'locks on'. And the character consumes her for a while.
This is very hard to explain from the outside. People often attribute it as a neurodivergent thing, but I think any lost, lonely person trying to figure themselves out can be vulnerable to this. Teenagers especially. I actually think most teenagers interested enough to enter a fandom are like this. Most of them, their brains are still cooking and they're predisposed to filter the world through a bit of a narcissistic lens. You know. "This character is just like me!" It's a natural life stage.
I've always been an outlier in fandom spaces. I tend to enjoy fiction for its own sake, and don't have that personal projected attachment to the characters. A character can be 'my favourite' and be someone completely appalling who has very little in common with me. I'm just fascinated by how they tick. Growing up in fandom spaces, I was the girl who wanted to dig into the flaws of my favourite character. Explore their feelings and sense of self. If we had had the tagging system we have today when I was a teenager, I would have been gunning straight for all the stories tagged "character study". But I soon learned that was rare. Most of my fandom friends were more interested in pretending the character didn't have any flaws, and getting defensive on their behalf. It was very important to them that everyone know their favourite did nothing wrong, actually, and every bad thing they'd ever done was some other character's fault. There tended to be a lot of arguments about that sort of thing.
It seems to have died down a bit these days, but now the need to project onto favourite characters has become more overt. I'm seeing a lot of self insert fanfiction, where you're supposed to simply swap out "Y / N" for your own name, and pretend it's about you. There seems to have been a rise in role play (people texting back and forth pretending to be the characters) and of course, there has been an absolute explosion of "trans headcanons". It's no longer at all unusual to look for art of a male character you know is not trans, and find him depicted with mastectomy scars in almost every picture.
Like I said, I've always been wired a little differently, so I'm a bit hamstrung in my understanding here. I can observe the behaviour of most teen girls in fandom and see the consistencies, but I don't fully 'get it'. I'm lacking that true inside perspective, of how it feels and what they're really thinking inside it all. I've never had that kind of illogical heightened attachment. Someone else might be able to explain that aspect better than me