I heard the Prophet ﷺ delivering a sermon, saying, "No man should be alone with a woman unless she is accompanied by a Muharram, and no woman should travel except with a Muharram."A man stood up and said, "Messenger of Allah, my wife has set out for Hajj, and I have been enlisted for such-and-such a military expedition."He replied, "Go and perform Hajj with your wife."
سمعت النبي ﷺ يخطب يقول: لا يخلون رجل بامرأة إلا ومعها ذو محرم، ولا تسافر المرأة إلا مع ذي محرم. فقام رجل فقال: يا رسول الله، إن امرأتي خرجت حاجة وإني اكتتبت في غزوة كذا وكذا؟ قال: انطلق فحج مع أهلك.
Bukhari narrated it in the chapter on the recompense of hunting (1862), and Muslim in the Book of Hajj (1341), both through the route of Amr bin Dinar, from Abu Mabad, the freed slave of Ibn Abbas, who said, "I heard Ibn Abbas say" (and he mentioned it), and the wording is Muslim's.