Those investments are not zero risk though. I don’t disagree with the general point but suggesting you should take a loan if the rate is less than the S&P return for the past year is questionable.
Nothing is zero risk, including owning a car; include the risk in your calculation. Your time frame is not one year, but typically 3-5. If your loan is at 2%, you don't need your stocks to hit 8% every year. You need them to be no worse than 2% p.a. over the entire period of your loan. That's significantly smaller risk than the risk of stocks not performing at your expectation of much higher return.
On average, the S&P 500 has historically returned roughly 9.8% annually. In 2017, the S&P 500's total return was over 19.7%. But for 2018, it was minus 6.2%.
You could easily achieve similar returns in an S&P 500 index fund.