Why did Yehudah and Bas Shu'a give their sons such derogatory names ('Er' - childless, and 'Onan' - mourner)?
Targum Yonasan: Because Er would indeed die without children and Onan would cause his father to mourn for him. 1
Ramban: Yehudah actually called his son 'Er' meaning to arouse, 2 and Bas Shu'a 3 called her second son 'Onan,' because she had a difficult birth. 4
Hadar Zekenim, citing a Midrash: Er's name should have been 'Ra,' 5 only he 6 reversed it to 'Er.'
Pesikta Zutresa (2): Yehudah was stricken, due to the sale of Yosef, through his wife and children. Their names hint to the sale. Er - [Yosef] was Hu'arah (emptied) from the world. Onan - He brought Aninus (mourning, to his father) over him. Shelah - (Yehudah) erred. He was in Keziv - He lied [about the sale].
Malbim: Bas Shu'a had not converted before they were born, and her sons' end was bad.
And Chazal say that when parents name their children, they are inspired by a spark of Ru'ach ha'Kodesh.
Ramban: As in "Orerah Es Gevurasecha" (Tehilim 80:3).
Hadar Zekenim (to 38:4): It was customary for the father to name the first son, and the mother, the second.
Ramban: As was common in those days. See 35:18, and Divrei Hayamim I 4:9.
Oznayim la'Torah: It suddenly struck him how he was going from one bad situation to another - (a) He advised the brothers to sell Yosef; (b) He was removed from his position of leadership (and lost his money); (c) He moved away from his brothers and his father's house, and (d) He married a Kena'anis and had a son from her.