Empathy from Shared Experience
All commentators agree that the primary motivation for this command is Israel's own history. Having been vulnerable 'sojourners in the land of Egypt,' they were expected to remember that difficult experience. This memory was meant to cultivate genuine sympathy and compassion for the foreigners living among them, reminding them to offer the welcome they once would have needed.