A primary residence shouldn't be compared to stock market investment on pure returns basis, as you get additional utility out of the home by living in it, using all that the neighborhood offers etc.
However, you can absolutely compare rental income (a kind of perpetuity, or an annuity with an indefinite payment stream duration) with another annuity (such as a dividend producing stock or a bond). In general, these all can (and in my opinion should) factor into your investment diversification strategy.
Real estate cash flows tend to be more volatile than say a bond from a AAA+ rated borrower (leases end, people stop paying, roof leaks, etc), but that should be measured against your required rate of return for the said risk.
When compared with stocks, real estate cash flows tend to be less risky (if valued properly), because even in disastrous situations you have some salvage value, whereas if a company goes bankrupt that's kind of that. (Gross oversimplification but still.)
One aspect of homeownership that I am a very big fan of is that even in a flat market, the mortgage acts as a type of forced savings account with negative interest (that is, you pay the bank). In a culture where the savings rates are dismally low, this is better than nothing. Unfortunately, refinancing and HELOCs really screwed this up for people. At the extreme of the last housing bubble, people were literally treating their home as an ATM...