Yes, you can. It's done all the time.
It can be a pleasure flight where the pilot brings along pax(s) and also an instructor to knock out a cross country or just for backup.
It can be a local training flight where the student's friend comes along just to observe.
You could also be doing a "revenue flight" in a jet with the owner on board and one pilot giving dual to the other along the way.
But there has to be a legit training purpose. You can't just log dual every time you get in an airplane.