There are basically 3 possibilities:
- Trump fired him.
(I don't think so, at least not explicitly)
- He's trying to grease his way back the corrupt and venal DC society, by pretending that he is a man of principle.
(My money is on this one. Barr has gotten what he wants out of the deal.)
- Trump is planning something so extreme and immoral that Barr felt that he had no choice but to resign.
(The man is completely immoral. I literally cannot imagine him finding an action to unethical for him to sanction.)
If there is any justice in this world, there will be a flood of complaints to the Maryland, Virginia, and DC Bar Associations against him.
It would be an obscenity for him to keep his law license after this.