One reason could be his sense of duty. Despite his dislike for Harry on the surface, he might still feel responsible for the safety of all students at Hogwarts, including Harry. So when he sees Harry in danger from Dementors, he steps in to save him.
Well, it could be that Snape has been secretly keeping an eye on Harry all along. In the fanfiction world, he might have developed a new spell or potion specifically designed to counter the effects of Dementors. When he sees Harry in danger, he uses this new creation to save him. For example, he could have a potion that creates a protective shield around Harry, preventing the Dementors from getting close enough to suck out his soul.
One possible reason could be that Snape still has an underlying sense of loyalty to Dumbledore. Since Dumbledore cares about Harry, Snape might step in to save him from Umbridge's cruelty. He doesn't want to see Hogwarts turned into a place of unjust persecution under Umbridge's regime, and Harry is a symbol of the resistance against that.
Snape might do it out of his love for Lily. Since Harry is Lily's son, he feels a sense of obligation to protect him.
One possible reason could be that Harry is afraid of Snape's intimidating presence. Snape has always been strict and rather unfriendly towards Harry in the original series, so in the fanfiction, this fear might be amplified, causing Harry to run. Another reason could be that Harry has stumbled upon something he shouldn't have seen that involves Snape, and he's running to avoid getting caught.
One possible reason could be Snape's lingering resentment towards James Potter, Harry's father. Snape might see Harry as a reminder of the bullying he endured from James during their school days. So, he takes out his anger on Harry.
One possible reason could be that Snape sees Hermione's value as a brilliant student. He might recognize her potential and not want it to be wasted just because of her dad's actions or situation. Maybe he has a sense of justice deep down, despite his often cold exterior.
One possible reason could be Snape's personal bias against Harry. Since Harry is James Potter's son, and Snape had a difficult relationship with James in their school days, Snape might unconsciously take out his old grudges on Harry.
In some fanfictions, it could be related to Snape's loyalty to Dumbledore. If Snape believes Harry is endangering a secret plan that Dumbledore has set in motion, he might punch Harry to stop him from doing something stupid. For example, if Harry is about to reveal a crucial piece of information to the wrong person.
One reason could be Harry's resemblance to his father. Snape had a long - standing grudge against James Potter, and seeing Harry might trigger those old feelings. In slash fanfiction, this can be exaggerated, with Snape being harsher on Harry because of his connection to James.
Another reason could be that Snape suspects Harry of some secret knowledge or activity. In the complex world of fanfiction, there could be hidden plots where Harry is involved in something that Snape is trying to uncover. By fingering Harry in class, he's trying to rattle Harry and get him to reveal something, either through his words or his actions during the class.