How to get category name dynamicaly on product page magento2?

Product can have multiple categories, like a product is selected in actual category, which could be 2nd or 3rd level, but the same product could also be selected in Sale category, or any other category.

So, in order to get actual category you can do this:

    $categoryCollection = clone $product->getCategoryCollection();
    $categoryCollection->clear();
    $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $storeManager->getStore()->getRootCategoryId() . "/%"));
    $categoryCollection->setPageSize(1);
    $treeCategories = $categoryCollection->getFirstItem()->getParentCategories();
    $tree = ();
    foreach ($treeCategories as $category) {
        $tree() = $category->getName();
    }

where $storeManager is the instance of MagentoStoreModelStoreManagerInterface, which usually send id 2, which is root category of your store.

Now in $tree, you will get the tree of your product category, like Grocery -> Food -> Rice