In "Harry Potter and the Prisoner of Azkaban" - when Harry and his friends were travelling to Hogwarts by train, the train stopped in between and Dementors were searching for Sirius Black. Why did one of the Dementors try to take Harry's soul?
They were never supposed to apply the Dementor's Kiss to any of the Hogwarts students. So that Dementor was acting out of line.
According to the Harry Potter wikia:
They seemed to be particularly attracted to Harry because, due to his miserable childhood and many bad memories, he was particularly vulnerable to their influence.
In other words, the Dementor saw a very tasty morsel, and its gluttony was stronger than its discipline.
From a storytelling point of view, this scene serves to show us a Dementor, and shows that they cannot be trusted to follow their orders to not harm the students.
The dementor was never trying to give harry the kiss. the same thing happened to harry as happened to everyone else: he relived his bad memories and felt cold. This was not an attempt to kiss harry. the dementors were only focused on harry because he had such sad memories, and dementors feed off of them