To the main question (why does Matthew mention this episode?) the only thing that can be said for certain is that Matthew, alone among the gospel writers, had heard the story (unless he made it up). So I would say that, since he knew of it, there was no good reason for him not to include it.
Other questions:
Did the Evangelist want to drive his readers home to the immense public pressure that Pilate was under to condemn Jesus?
Possibly. This attitude is mentioned by Matthew Henry, for example, who says: "The Jews were so bent upon the death of Christ, that Pilate thought it would be dangerous to refuse."
Or, was it intended to show how hardcore Pilate was that in spite of getting warned, he went ahead to appease Jewish leaders?
I would answer no to this. Rather, Matthew portrays Pilate as being ready to let Barabbas suffer instead of Jesus. Shortly after mentioning the dream of Pilate's wife, Matthew relates:
24 When Pilate saw that he was not succeeding at all, but that a riot
was breaking out instead, he took water and washed his hands in the
sight of the crowd, saying, “I am innocent of this man’s blood. Look
to it yourselves.”
How do scholars explain the relevance of Mt. 27:19 mentioning the post-dream message of Pilate's wife?
Responses fall into four main groups:
- The dream came from the devil in order to frustrate God's plan, which was to sacrifice Jesus on the Cross. Ellicott and Calvin mention this possibility, but do not endorse it. Gill provides the counter argument:
Some have thought, that this dream was from the devil, willing to
hinder the death of Christ, and so man's redemption and salvation by
it; but had he had any such intention, the most effectual method would
have been to have persuaded the chief priests and elders off of it...
- The dream came from God in order to emphasize Jesus' innocence. Calvin is one example:
We ought to conclude that God the Father took many methods of attesting the innocence of Christ, that it might evidently appear that he suffered death in the room of others, — that is, in our room. God intended that Pilate should so frequently acquit him with his own mouth before condemning him, that in his undeserved condemnation the true satisfaction for our sins might be the more brightly displayed.
- The story of the dream was a legend that Matthew had heard. Meyers does not agree with this, but he mentions several scholars who do.
There is nothing to show that Matthew intended us to regard this
incident as a special divine interposition. There is the less reason
for relegating it to the domain of legend (Strauss, Ewald, Scholten,
Volkmar, Keim). {df - This is also the view of more recent scholars such as Crossan and Spong}
Conclusion: Although there is no consensus, the majority view is that Matthew relates the dream of Pilate's wife to emphasize Jesus' innocence.