Genesis 38 is the story of Tamar, a Canaanite. Tamar’s husband dies without an heir. So Tamar must protect the family line through an ancient custom called Levirate marriage. She accomplishes her task against the will of her father-in-law Judah (who also married a Canaanite).
By the narrative’s conclusion, Judah has declared that Tamar acted more righteously than he for protecting her husband’s lineage. By extension, she also protected the Abrahamic Covenant at the heart of Israel’s identity and claim to the Promise Land (Genesis 15, 17). Consequently, all of Judah descend from this mix of the Hebrew and Canaanite races, including David and Jesus. These meager beginnings of diversity foreshadow something much greater in Exodus.