Perhaps he is under some sort of dark magic influence. There are many dark spells and artifacts in the Harry Potter universe. If he's been cursed or affected by some dark object that messes with his mind, it could make him attempt suicide in the fanfic.
One possible reason could be extreme depression. Maybe in the fanfic, Harry Potter has faced so many losses and hardships that he feels there's no way out. For example, if he lost all his friends and felt alone in the fight against dark forces, it could push him to such a dark place.
It could be due to a sense of failure. Suppose he fails to protect Hogwarts or his loved ones from some great threat. In the world of Harry Potter, he has a huge responsibility. If he feels he has let everyone down, that might lead to suicidal thoughts in the fanfic.
In fanfics where Naruto attempts suicide, it could be due to extreme loneliness. He has often faced isolation in the original story, and in some fan interpretations, that feeling could be amplified to a point where he sees no way out. Another reason might be failure. If he fails in protecting his friends or achieving his goals in the fanfic's alternate storyline, he could feel so despaired that he contemplates suicide.
Well, it could be due to a sense of isolation. Harry has always been somewhat of an outcast, even within the wizarding community. He might feel that he can't truly connect with others on a deep level. Additionally, in some fanfic interpretations, the author might be showing how the prophecy and his role as 'the Chosen One' has burdened him to the point of despair. It's like he has no control over his own life and destiny, which could lead to suicidal thoughts.
One reason could be a misinterpretation of the characters' struggles. In the original series, the characters face many difficulties, but they always find a way to persevere. However, some fanfiction writers may take those struggles too far and end up with the idea of suicide. Also, there might be a lack of understanding of the impact such content can have on the readers, especially the younger ones.
One reason could be to explore the darker side of Harry's character. In the original books, he faced a lot of trauma, and this could be an extreme way to further analyze how that could potentially affect him.
The original Harry Potter series is mainly about hope, friendship, and the fight against evil. Suicide is a very dark and hopeless act, which is completely opposite to the overall positive and hopeful tone of the original. In the original, even in the darkest times, Harry and his friends always find a way to keep going.
One reason could be the strict control by the official copyright holders. They might be worried about fanfic diluting the official brand or misinterpreting the original story. Another factor could be some fans' purist view. They only want to stick to the original works by J.K. Rowling and consider fanfic as something not in line with the 'true' Harry Potter universe.
One possible reason could be extreme despair. Maybe in that fanfic's alternate universe, Harry has lost all his friends and loved ones in a very tragic way, like a second wizarding war that was far more brutal and unforgiving. He might see no way out of the emotional pain and thus choose to end his own life.
It could also be that the fanfic might interfere with the way he wants his story to be remembered. His adventures at Hogwarts were real and important to him, and when fanfic changes the narrative, it's like his story is being disrespected. For example, if a fanfic portrays him as cowardly when he was actually brave, that would surely make him dislike it.
One reason could be a personal betrayal. Maybe a close friend from Gryffindor betrays Harry, and this makes him start to dislike the whole house. It's like when one bad apple spoils the bunch in his eyes.
Another reason might be jealousy. If a character in the fanfic is trying to do something related to Harry Potter, like write a book about him or start a movement in his name, others who are jealous of their potential success could send rejection letters to thwart their efforts.