According to at least one source in the Nixon administration, it was because it was a good filter for the kinds of people that resist capitalism (non-conforming white people and minorities). Make that illegal, and you have a pretext for throwing people in jail that might cause trouble.
https://eji.org/news/nixon-war-on-drugs-designed-to-criminal...
"
In a 1994 interview, Mr. Ehrlichman said, “You want to know what this was really all about?” He went on:
“The Nixon campaign in 1968, and the Nixon White House after that, had two enemies: the antiwar left and Black people. You understand what I’m saying? We knew we couldn’t make it illegal to be either against the war or Black, but by getting the public to associate the hippies with marijuana and Blacks with heroin, and then criminalizing both heavily, we could disrupt those communities. We could arrest their leaders, raid their homes, break up their meetings, and vilify them night after night on the evening news. Did we know we were lying about the drugs? Of course we did.”
"
One reason it's becoming legal now is because it is ceasing to be as good of a filter.