It wasn’t a coincidence
…הכר-נא הכתונת בנך הוא אם-לא: …הכר-נא למי החתמת והפתילים והמטה האלה
Yehudah: “…Please identify if this coat belongs to [Yosef] or not”.
Tamar: “… Please identify who owns this seal, cloak and staff”
After Yaakov’s sons decided to sell their brother Yosef into slavery, they had to create a coverup story to tell their father. They decided to take Yosef’s coat, dip it in goat’s blood, and show it to their father. He would hopefully intuit Yosef was dead. Yehudah asked his father if he recognized the coat, and Yaakov assumed a wild animal had eaten Yosef. He was devastated by this news, and refused to accept any comfort from his family. Twelve years later a seemingly unconnected story occurs with Yehudah.
After his son Er married Tamar, Er died suddenly. Yehudah’s other son Onan similarly died after subsequently marrying Tamar. Tamar was presumed to marry Yehudah’s third son Sheilah, but Yehudah was worried he would meet the same fate as his late brothers. He decided to push her off, delaying the marriage. Tamar got fed up waiting, as she wanted to have children. She decided to disguise herself as a harlot, and propositioned Yehudah. He fell for it, and didn’t recognize who he was with. She demanded from Yehudah collateral to secure her payment. He gave her his seal, cloak, and staff. Having successfully fooled her father in law, the “harlot” disappeared. Yehudah later tried to retrieve his items, but couldn’t find her. About three months later, it was clear to all that Tamar was pregnant.
As she was technically engaged to Yehudah’s third son, Yehudah ordered the authorities to have her punished for her infidelity. As she was being taken, she asked Yehudah a question similar to one that Yehudah had asked twelve years earlier. “Do you recognize these items?” Yehudah noticed that they were his, realized what had happened, and had her acquitted. Tamar later gave birth to twin boys, one of which would be the progenitor to the Moshiach. Chazal pick up on the fact that Tamar asked Yehudah a very similar question to the one Yehudah had asked his father Yaakov. The way they phrase it is בהכר בשר, בהכר בשרוהו, he informed Yaakov with “identify”, so they informed him with “identify”. What are Chazal conveying with this teaching? They are showing it wasn’t merely coincidence, but is there more to the story?
It sounds like this is considered some sort of punishment. Hashem deals with people middah keneged middah, measure for measure. If someone does something wrong, Hashem makes sure that the same thing happens to them. Just like Yehudah conned his father into thinking the cloak meant Yosef was eaten by a lion, so too Tamar conned him into thinking she was a harlot. Since he caused pain to Yaakov, Tamar’s actions caused him pain. This wasn’t simply coincidence. Hashem was actively involved in engineering the sequence of events, to teach Yehudah a lesson.
Others have a fancier explanation. Chazal inform us that Tamar was a descendant of Shem, the son of Noach. Shem was the first Kohen, and due to an impropriety he committed, his status was transferred to Avraham’s descendants, starting with Aharon. This creates a question with Tamar. When Yehudah ordered the authorities to have Tamar punished for her infidelity, she was going to get the punishment suitable for the daughter of a Kohen. If Shem’s Kohen status was removed, why would she be considered the daughter of a Kohen? The answer is there is a fundamental principle in Judaism that there are always exactly twelve tribes. When the tribe of Levi was separated from their brothers to be their own special tribe consisting of Kohanim and Leviim, Yosef’s tribe was split into two: Efraim and Menashe. However, when Yaakov’s sons rejected Yosef and sold him to Egypt, there were now only ten tribes. Even if Reuven, who had two sons, had his tribe split into two, that would only make eleven tribes. Therefore, Levi had to lose his special status to be counted again amongst his brothers, making twelve. When that happened, Shem and his descendants regained their status. As Tamar was about to be punished as a daughter of a Kohen, she asked Yehudah if he recognized his items. It turns out then that the climax of this episode only could occur because of the brothers’ plot to sell Yosef. The climax of that plot was Yehudah showing the bloodied coat to Yaakov. It turns out then that Chazal aren’t indicating a case of measure for measure, rather a realization of cause and effect.
A final explanation takes a more sophisticated approach. The sale of Yosef, led by Yehudah, in the end had a positive outcome. Yosef became the viceroy of Egypt, and through his wisdom was able to foresee a terrible famine. He used his power to store food and make the necessary preparations. This allowed his family to survive what would have surely caused their demise. In the larger scheme of things, the descent of Yaakov and his family to Egypt began the whole process of receiving the Torah. After being enslaved for 210 years, Moshe redeemed the Jews and took them to Mount Sinai to receive the Torah. This all started with the sale of Yosef. However, Yehudah and his brothers didn’t necessarily have all this in mind when they sold him. Selling their brother as a slave, while in the end was for the best, was considered on their part a sin. The way things work is one sin leads to another similar to it. Tamar in the end was the proper mate for Yehudah. One of the children they had together became the progenitor of Moshiach. However, Yehudah at the time thought she was a harlot. He didn’t know this good was come from this act, which on the surface was considered a sin. This is what Chazal were referring to when they said בהכר בשר בהכר בשרוהו. One sin caused another one similar to it. Even though the outcomes were positive, they were still sins.
 Based on various sources that I found and collected
 Even though the speaker isn’t identified, all the midrashim assume it was Yehudah. Mattanos Kehunah to Bereishis Rabbah 84:19 says that there is no doubt it was Yehudah, as he was their leader. Maharsha to Sotah 10b says he was the most prominent of Yaakov’s sons (Torah Temimah Chapter 38 § 30 says similarly), and the whole idea to sell Yosef was his. However, as noted by the Rashash to the Midrash ad. loc., Bereishis Rabbah 84:8 says the brothers drew lots who would have to tell Yaakov what happened. Yehudah’s lot was selected
 Genesis 37:32, 38:25
 ibid 37:31-35
 Seder Olam Chapter 2; Da’as Zekeinim and Chizukuni to Genesis 38:1. The calculation is Yehudah immediately got married, and his wife took a year to give birth to Er. Er got married at seven to Tamar. A year later, Onan married Tamar (after Er had died). A year later Yehudah told Tamar to wait to marry Sheilah (after Onan had died). A year later Yehudah’s wife died, and Tamar’s ruse occurred. Although, Chizkuni seems to explain Er died right away, making it eleven years. Cf. Ibn Ezra ad. loc. who doesn’t see how all these events could happen in such a short time, so he as well as Lekach Tov and Socher Tov explain that these events happened before the sale of Yosef
 Genesis Chapter 38
 Based on ibid verse 8, many commentaries (such as Ramban to verse 11, Rashba to Yevamos 98) say that it was due to the obligation of yibbum. See Shir HaShirim Rabbah 1:16 and Pesiktah D’Rav Kehana Chapter 12 which say that Yehudah was obligated through prophecy to observe yibbum. See also Nodeh BeYehudah I Even HaEzer § 10 and Chasam Sofer to Genesis 38:26 who based on this, give an interesting explanation why Yehudah wouldn’t let Tamar marry Sheilah
 Peretz. See Ruth 4:18-22 that Dovid Hamelech’s lineage comes from Peretz.
 Sotah 10b, brought by Rabbeinu Bechaye to Genesis 37:3 and Ba’al Haturim to verse 32; Bereishis Rabbah 85:9
 Nedarim 32a. Torah Temimah loc. cit. cites the related idea of במדה שאדם מודד בה מודדין לו (Sotah 1:7)
 Maharsha loc. cit. There’s a similar idea that he cites from Bereishis Rabbah 85:9. Eitz Yosef to Sotah 10b also explains the Midrash like this.
 I would have said because the realization that he was the one who impregnated her and that she had fooled him would have been embarrassing and a blow to his confidence. However, the Maharsha and Eitz Yosef to the Midrash ad. loc. say the pain she caused him was by disappearing with his items. The verse says that he tried to find her but to no avail, conveying his frustration
 Eitz Yosef to Sotah loc. cit. He takes it a step further and points out that goats are involved in both events. Yosef’s coat was dipped in goat blood, and Tamar’s payment was supposed to be goats (Genesis 38:17). This idea comes from Bereishis Rabbah 85:9, cited by Rashi to verse 23. Eitz Yosef also says that you can interpret both Yehudah and Tamar’s statements of הכר נא as having positive intentions (see note 23)
 Chasam Sofer’s Toras Moshe I to Genesis 38:25
 Bereishis Rabbah 85:9 (brought by Rashi to ibid verse 24) and Yalkut Shimoni to Nach § 549 say she was the daughter of Shem. The commentaries (for example Mizrachi and Minchas Yehudah ad. loc., Chasam Sofer loc. cit.) explain that it must really mean a descendant, otherwise the timelines don’t match up. In fact, Sotah 10a clearly says she was a child in this story. Rashi ad. loc. explains if she was a daughter of Shem she would have to be an old woman. Either it’s a dispute between midrashim (Mizrachi loc. cit. also suggests this), or the daughter of Shem really means descendant. However, Tosafos in Kiddushin 5a and Sukkah 24b s.v. על מנת clearly hold that she was literally the daughter of Shem
 Genesis 14:18
 Ramban to Genesis 49:22
 See Torah Sheleimah Chapter 38 § 96 for a list of various other approaches to how Tamar could be punished like the daughter of a Kohen even if she didn’t really have that status. However, Mizrachi and Chizkuni to Genesis 38:24 simply explain that only Shem’s children lost their status as Kohanim. Shem kept it, making Tamar a daughter of a Kohen. However, see note 15
 Meshech Chochmah to Genesis 37:26
 Genesis 41:48
 See Rashi to ibid 42:2
 Cf. Iyun Yaakov to Sotah loc. cit. who says the connection between both stories is they had good intentions but the action itself was bad
 See Kiddushin 81b that says that someone who thinks they’re doing something forbidden but are really doing something good still needs atonement. The Meshech Chochmah brings an opposite example of this idea from Aharon, who made the Golden Calf in order to stall for time. He had good intentions, but the act itself was considered a sin on his part. He therefore also needed atonement
 Avos 4:2
 Rav Tzvi Berkowitz explained to me that all the Meshech Chochmah means is Chazal are teaching us that these seemingly incidental details which are parallel are really showing us the idea expressed in the Mishnah in Avos loc. cit.
 See Makkos 22b with Rashi regarding ממני, כבושים הדברים, that the episode with Yehudah and Tamar happened because Hashem willed it
 See note 8
 The Torah Temimah loc. cit. says a similar idea, that both episodes resulted in something positive, but were carried out in a degrading way