When you rename a block entity (barrel, chest, enchantment table etc) on an anvil, it has its name like it's supposed to, even when placed. However, when you mine it to pick it up, it no longer stacks with the containers from the anvil, because it lost the "RepairCost:0" tag. So you end up with two different block entities with the same name, unable to stack them because of a tag that gets lost when it's placed.
1. Rename a stack of barrels/chest/etc on an anvil
2. Place a few of the containers
3. Mine containers in survival mode