Among Jewish people, it’s called the “King David” effect.
King David did lots of bad things. Very bad things. He had a zipper problem (er, button? robe? ties?) that led to effectively a murder of a good man.
While a deeply flawed man, he was also exactly the man needed for the time.
Trump is similar, on a smaller scale.
I always think of Jehu when I think of flawed kings chosen by God to do a job. Jehu was tasked with getting rid of the house of Ahab and the Baal worshippers. He killed them all, and I think he turned to idol worship in his later years, but he was the man for the job at the time.